package com.mybank.myaccounts.mapper;

import com.mybank.common.entity.AccountApplication;
import com.mybank.common.entity.AccountType;
import com.mybank.common.entity.Customer;
import com.mybank.myaccounts.dto.AccountApplicationDto;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class AccountApplicationMapper {
    
    /**
     * Convert entity to DTO
     */
    public AccountApplicationDto toDto(AccountApplication application) {
        if (application == null) {
            return null;
        }
        
        return AccountApplicationDto.builder()
                .applicationId(application.getApplicationId())
                .customerId(application.getCustomer() != null ? application.getCustomer().getCustomerId() : null)
                .accountTypeId(application.getAccountType() != null ? application.getAccountType().getAccountTypeId() : null)
                .accountTypeName(application.getAccountType() != null ? application.getAccountType().getName() : null)
                .applicationDate(application.getApplicationDate())
                .status(application.getStatus())
                .initialDeposit(application.getInitialDeposit())
                .purpose(application.getPurpose())
                .sourceOfFunds(application.getSourceOfFunds())
                .rejectionReason(application.getRejectionReason())
                .approvedBy(application.getApprovedBy())
                .approvalDate(application.getApprovalDate())
                .notes(application.getNotes())
                .createdAt(application.getCreatedAt())
                .updatedAt(application.getUpdatedAt())
                .build();
    }
    
    /**
     * Convert DTO to entity
     */
    public AccountApplication toEntity(AccountApplicationDto dto, Customer customer, AccountType accountType) {
        if (dto == null) {
            return null;
        }
        
        return AccountApplication.builder()
                .applicationId(dto.getApplicationId())
                .customer(customer)
                .accountType(accountType)
                .applicationDate(dto.getApplicationDate())
                .status(dto.getStatus())
                .initialDeposit(dto.getInitialDeposit())
                .purpose(dto.getPurpose())
                .sourceOfFunds(dto.getSourceOfFunds())
                .rejectionReason(dto.getRejectionReason())
                .approvedBy(dto.getApprovedBy())
                .approvalDate(dto.getApprovalDate())
                .notes(dto.getNotes())
                .build();
    }
    
    /**
     * Convert list of entities to list of DTOs
     */
    public List<AccountApplicationDto> toDtoList(List<AccountApplication> applications) {
        if (applications == null) {
            return null;
        }
        
        return applications.stream()
                .map(this::toDto)
                .collect(Collectors.toList());
    }
}